import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PassportModule } from '@nestjs/passport';
import { APP_GUARD } from '@nestjs/core';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { GroceryModule } from './module/grocery/grocery.module';
import { CustomModule } from './module/custom/custom.module';
import { jwtConstants } from './utils/constant';
import { AuthService } from './auth/auth.service';
import { LocalStrategy } from './auth/local.strategy';
import { JwtStrategy } from './auth/jwt.strategy';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
import { LocalCustomStrategy } from './auth/local-custom.strategy';

const envFilePath = ['.env'];
if (process.env.NODE_ENV) {
  envFilePath.unshift(`.env.${process.env.NODE_ENV}`);
}
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath,
      // load:
      // envFilePath: ['.env', '.env.dev', '.env.stage', '.env.prod'],
    }),

    // PassportModule,
    // JwtModule.register({
    //   secret: jwtConstants.secret,
    //   signOptions: { expiresIn: '10s' },
    // }),

    TypeOrmModule.forRootAsync({
      name: 'shop',
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          type: configService.get('TYPEORM_CONNECTION') as 'mysql',
          host: configService.get('TYPEORM_HOST'),
          port: configService.get('TYPEORM_PORT'),
          username: configService.get('TYPEORM_USERNAME'),
          password: configService.get('TYPEORM_PASSWORD') as string,
          database: configService.get('TYPEORM_DATABASE') as string,
          autoLoadEntities: true,
          synchronize: configService.get('TYPEORM_AUTO_SCHEMA_SYNC'),
          retryAttempts: 20,
        };
      },
    }),

    GroceryModule,
    CustomModule,
  ],

  providers: [
    // AuthService,
    // LocalStrategy,
    // LocalCustomStrategy,
    // JwtStrategy,
    
    JwtService,
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
  ],
  exports: [
    // AuthService
  ]
})
export class AppModule {}
